package com.wnhz.mayabunny;

import java.util.ArrayList;
import java.util.List;

import com.wnhz.mayabunny.fragment.All_Service;
import com.wnhz.mayabunny.fragment.Home_pager;

import com.wnhz.mayabunny.fragment.Myself_initialise;
import com.wnhz.mayabunny.fragment.News;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {
	private All_Service tasks_all;
	private Home_pager home_pager;
	private Myself_initialise myself;
	private News news;
	private ImageView home;
	private ImageView tasks;
	private ImageView new_new;
	private ImageView myself_;
	private TextView home_TextView;
	private TextView tasks_TextView;
	private TextView new_TextView;
	private TextView myself_TextView;
	private ViewPager viewPager;
	private List<Fragment> fragments = null;

	private int currenttab = -1;
	private int screenWidth;
	private int normalColor = 0xff000000, selectedColor = 0xffff6b47;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
	
		into();
		fragments.add(home_pager);
		fragments.add(tasks_all);

		fragments.add(news);
		fragments.add(myself);
		home.measure(0, 0);
		viewPager.setAdapter(new MyFrageStatePagerAdapter(getSupportFragmentManager()));
		viewPager.setOffscreenPageLimit(3);

	}

	private void into() {
		fragments = new ArrayList<Fragment>();

		home_pager = new Home_pager();
		tasks_all = new All_Service();
		news = new News();
		myself = new Myself_initialise();
		viewPager = (ViewPager) findViewById(R.id.yemian);
		screenWidth = getResources().getDisplayMetrics().widthPixels;
		home = (ImageView) findViewById(R.id.home_page);
		home_TextView = (TextView) findViewById(R.id.home_page_text);
		tasks = (ImageView) findViewById(R.id.all_tasks);
		tasks_TextView = (TextView) findViewById(R.id.all_tasks_text);
		new_new = (ImageView) findViewById(R.id.all_news);
		new_TextView = (TextView) findViewById(R.id.all_news_txet);
		myself_ = (ImageView) findViewById(R.id.myself);
		myself_TextView = (TextView) findViewById(R.id.myself_text);
		home.setOnClickListener(this);
		tasks.setOnClickListener(this);
		new_new.setOnClickListener(this);
		myself_.setOnClickListener(this);
		into_activty();
	}

	public void into_activty() {
		changeView(0);
		text_color();

		home_TextView.setTextColor(selectedColor);
		home.setSelected(true);
		tasks.setSelected(false);
		new_new.setSelected(false);
		myself_.setSelected(false);
	}

	public void text_color() {
		home_TextView.setTextColor(normalColor);
		tasks_TextView.setTextColor(normalColor);
		new_TextView.setTextColor(normalColor);
		myself_TextView.setTextColor(normalColor);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.home_page:
			changeView(0);
			text_color();
			home_TextView.setTextColor(selectedColor);
			home.setSelected(true);
			tasks.setSelected(false);
			new_new.setSelected(false);
			myself_.setSelected(false);

			break;
		case R.id.all_tasks:
			// tasks.setImageResource(R.drawable.zhinanzheng1);
			changeView(1);
			text_color();
			tasks_TextView.setTextColor(selectedColor);
			tasks.setSelected(true);
			home.setSelected(false);
			new_new.setSelected(false);
			myself_.setSelected(false);

			break;
		case R.id.all_news:
			changeView(2);
			text_color();
			new_TextView.setTextColor(selectedColor);
			new_new.setSelected(true);
			home.setSelected(false);
			tasks.setSelected(false);
			myself_.setSelected(false);

			break;
		case R.id.myself:
			changeView(3);
			text_color();
			myself_TextView.setTextColor(selectedColor);
			myself_.setSelected(true);
			home.setSelected(false);
			tasks.setSelected(false);
			new_new.setSelected(false);

			break;
		default:
			break;
		}

	}

	private void changeView(int desTab) {
		viewPager.setCurrentItem(desTab, true);
	}

	class MyFrageStatePagerAdapter extends FragmentStatePagerAdapter {

		public MyFrageStatePagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}

		@Override
		public int getCount() {
			return fragments.size();
		}

		public void finishUpdate(ViewGroup container) {
			super.finishUpdate(container);
			int a = viewPager.getCurrentItem();
			switch (a) {
			case 0:
				text_color();
				home_TextView.setTextColor(selectedColor);
				home.setSelected(true);

				tasks.setSelected(false);
				new_new.setSelected(false);
				myself_.setSelected(false);
				break;

			case 1:
				text_color();
				tasks_TextView.setTextColor(selectedColor);
				tasks.setSelected(true);
				home.setSelected(false);
				new_new.setSelected(false);
				myself_.setSelected(false);
				break;

			case 2:
				text_color();
				new_TextView.setTextColor(selectedColor);
				new_new.setSelected(true);
				home.setSelected(false);
				tasks.setSelected(false);
				myself_.setSelected(false);
				break;
			case 3:
				text_color();
				myself_TextView.setTextColor(selectedColor);
				myself_.setSelected(true);
				home.setSelected(false);
				tasks.setSelected(false);
				new_new.setSelected(false);
				break;
			default:
				break;
			}

		}

	}

}
